package com.sunny.shop.service;

import com.sunny.shop.core.LocalUser;
import com.sunny.shop.dto.VaccinationDTO;
import com.sunny.shop.dto.VaccineDTO;
import com.sunny.shop.exception.httpe.NotFoundException;
import com.sunny.shop.model.Vaccination;
import com.sunny.shop.repository.VaccinationRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VaccinationServiceImpl {
    @Autowired
    private VaccinationRepository vaccinationRepository;
    public Integer doTag(VaccinationDTO vaccinationDTO){
        Long uid = LocalUser.getUser().getId();
        Long vaccineId = vaccinationDTO.getVaccineId();
        Vaccination result = vaccinationRepository.findOneByVaccineIdAndUid(vaccineId, uid);
        Integer msgCode;
        if(result != null){
            Integer status = result.getStatus();
            Integer newStatus = status == 1 ? 0 : 1;
            vaccinationDTO.setStatus(newStatus);
            msgCode = status == 1 ? 10004 : 10003;
            BeanUtils.copyProperties(vaccinationDTO,result);
            vaccinationRepository.save(result);
        }else{
            vaccinationDTO = vaccinationDTO.builder().status(1).vaccineId(vaccineId).uid(uid).build();
            Vaccination vaccination = new Vaccination();
            BeanUtils.copyProperties(vaccinationDTO, vaccination);
            vaccinationRepository.save(vaccination);
            msgCode = 10003;
        }
        return msgCode;
    }
}
